package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.business.domain.DailyTrainCarriage;
import com.myc.train.business.domain.Train;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.DailyTrain;
import com.myc.train.business.domain.DailyTrainExample;
import com.myc.train.business.mapper.DailyTrainMapper;
import com.myc.train.business.req.DailyTrainQueryReq;
import com.myc.train.business.req.DailyTrainSaveReq;
import com.myc.train.business.resp.DailyTrainQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainService {

    @Resource
    private DailyTrainMapper dailyTrainMapper;

    @Resource
    private TrainService trainService;

    @Resource
    private DailyTrainStationService dailyTrainStationService;

    @Resource
    private DailyTrainCarriageService dailyTrainCarriageService;

    @Resource
    private DailyTrainSeatService dailyTrainSeatService;

    @Resource
    private DailyTrainTicketService dailyTrainTicketService;

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainService.class);

    public void save(DailyTrainSaveReq req){
        DateTime now = DateTime.now();
        DailyTrain dailyTrain = BeanUtil.copyProperties(req, DailyTrain.class);

        if(ObjectUtil.isNull(dailyTrain.getId())){
            dailyTrain.setId(SnowUtil.getSnowflakeNextId());
            dailyTrain.setCreateTime(now);
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.insert(dailyTrain);
        }else{
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.updateByPrimaryKey(dailyTrain);
        }
    }

    public PageResp<DailyTrainQueryResp> queryList(DailyTrainQueryReq req){
       //定义查询条件
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        //根据id倒序排序
        dailyTrainExample.setOrderByClause("date desc , code desc");
        //创建查询对象
        DailyTrainExample.Criteria criteria = dailyTrainExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        if(ObjectUtil.isNotEmpty(req.getCode())){
            criteria.andCodeEqualTo(req.getCode());
        }

        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<DailyTrain> dailyTrains = dailyTrainMapper.selectByExample(dailyTrainExample);
        //new一个PageInfo并将结果放进来
        PageInfo<DailyTrain> dailyTrainPageInfo = new PageInfo<>(dailyTrains);
        //类拷贝
        List<DailyTrainQueryResp> queryResps = BeanUtil.copyToList(dailyTrains, DailyTrainQueryResp.class);
        //new一个PageResp
        PageResp<DailyTrainQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(dailyTrainPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        dailyTrainMapper.deleteByPrimaryKey(id);
    }


    //生成每日的车次信息，包括车次、车站、车厢、座位
    public void genDaily(Date date){
        //获取车次列表
        List<Train> trains = trainService.selectAll();
        if(CollUtil.isEmpty(trains)){
            LOG.info("当前无车次信息");
            return;
        }
        for(Train train: trains){

            genDailyTrain(date, train);
        }
    }

    private void genDailyTrain(Date date, Train train) {
        //有数据删除当日的车次数据
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.createCriteria().andDateEqualTo(date).andCodeEqualTo(train.getCode());
        dailyTrainMapper.deleteByExample(dailyTrainExample);

        //新增当日数据
        DateTime now = DateTime.now();
        DailyTrain dailyTrain = BeanUtil.copyProperties(train, DailyTrain.class);
        dailyTrain.setId(SnowUtil.getSnowflakeNextId());
        dailyTrain.setCreateTime(now);
        dailyTrain.setUpdateTime(now);
        dailyTrain.setDate(date);
        dailyTrainMapper.insert(dailyTrain);

        //新增当日车次的车站信息
        dailyTrainStationService.genDailyTrain(date,train.getCode());

        //新增当日车厢信息
        dailyTrainCarriageService.genDailyTrain(date, train.getCode());

        //新增当日车座信息
        dailyTrainSeatService.genDailyTrain(date, train.getCode());

        //新增当日余票信息
        dailyTrainTicketService.dailyTrainCode(dailyTrain , date, train.getCode());
    }

}
